Control How Models Load Input Data

您所在的位置:网站首页 simulink的from workspace Control How Models Load Input Data

Control How Models Load Input Data

#Control How Models Load Input Data | 来源: 网络整理| 查看: 265

Open and Examine Model

Open the model LoadInputData. The model uses an Inport block to load external input data from the base workspace.

mdl = "LoadInputData"; open_system(mdl)

The Input parameter is configured to load data from the variable simIn.

get_param(mdl,"LoadExternalInput")ans = 'on' get_param(mdl,"ExternalInput")ans = 'simIn'

Create and Format Input Data

Create input data for a sine wave in the base workspace. To start, create an evenly spaced time vector to pass to the sin function to generate the data values.

When you create data to load as a discrete signal, use the expression in this example. Other techniques for creating an evenly spaced time vector, such as the linspace function or using the colon operator with a specified increment (a:b:c), can introduce floating-precision rounding errors that can lead to unexpected simulation results.

sampleTime = 0.1; numSteps = 101; time = sampleTime*(0:numSteps-1); time = time';

Create the signal values by using the sin function to create a value for each element in the time vector.

data = sin(2*pi/3*time);

Format the workspace data to load into the simulation. When you load input data for a discrete signal, you can avoid the effect of floating-precision rounding errors in data you provide by using the structure without time format.

When you use the structure without time format, you do not specify time data along with the signal values. Instead, the simulation associates time values with the signal values using the sample time you specify for the block. During simulation, the block loads the input values sequentially at the specified rate.

The structure format has two fields:

time: A common time vector for all signals in the structure

signals: An array of structures that contain the data and dimensions information for each signal

Create the structure simIn to load the sine wave data into the model using the structure without time format.

simIn.signals.values = data; simIn.signals.dimension = 1; simIn.time = [];

Configure Block to Produce Discrete Signal

Configure the block to use a sample time of 0.1 and use zero-order-hold interpolation.

Select the Inport block.

Open the Property Inspector by clicking the Property Inspector tab on the right of the model or by pressing Ctrl+Shift+I.

Expand the Execution section.

Set the Sample time to 0.1.

Clear Interpolate data.

Alternatively, use the set_param function to configure the block.

set_param(strcat(mdl,"/Inport"),"SampleTime","0.1",... "Interpolate","off")

Simulate Model

Simulate the model.

out = sim(mdl);

The Dashboard Scope block displays the sine wave signal. The zero-order hold creates a stair step effect between each sample value.

Use Format with Time Data

When you create the evenly spaced time vector yourself, using the expression in this example, you can also load input data using formats that include time values without floating-precision rounding issues. For example, create a timetable using the time and signal values for the sine wave.

secs = seconds(time); simIn = timetable(secs,data);

Simulate the model again.

out2 = sim(mdl);

The results on the Dashboard Scope look the same as the previous simulation.

The values in the time vector are the same for the simulation that used the structure without time format and the simulation that used the timetable format with a time vector created using the expression in this example.

isequal(out.yout{1}.Values.Time,out2.yout{1}.Values.Time)ans = logical 1


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3